/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thePieces;

import Headquarters.viewPerspective.perspective;
import anotherFrameWork.line;
import anotherFrameWork.point;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
 *
 * @author Windows7
 */
    


public class theSpritePieces implements Runnable
{

    /**
     * @param desPlace the desPlace to set
     */
    public void setDesPlace(point desPlace) {
        this.desPlace = desPlace;
    }





    public enum kindOfPieces
    {
        nothingHere, king, queen, knight, bishop, rook, pawn; 
    }    
    
    public enum colorOfPiexes
    {
        black, white;
    }
    
    //protected Graphics2D bufferGraphic;
    
    //protected Image buffer,backBuffer;
    
    protected perspective view;
    
    // vị trí hiện tại của quân cờ
    protected point inPlace;
    
    // vi trí đích đến của quân cờ.
    private point desPlace;
    
    
    private colorOfPiexes side;
    
    // hình ảnh quân cờ;
    protected BufferedImage img;
    
    //con đường di chuyển 
    protected line wayToGo;
    
    int count = 0;
    public theSpritePieces(perspective view)
    {
        this.inPlace = new point();
        this.desPlace = new point();
        this.wayToGo = new line(inPlace,desPlace);
        this.side = null;
        this.view = view;
        this.img = null;
    }
    
    public theSpritePieces(String str, point p, colorOfPiexes side,perspective view)
    {
        this.view = view;
        this.side = side;
        this.inPlace = new point(p.getX(),p.getY());
        this.desPlace = new point(p.getX(),p.getY());
        this.wayToGo = new line(this.inPlace,this.desPlace);
        //this.setBounds(0, 0, 37, 37);
        //this.setBackground(Color.red);
        //this.setBorder(BorderFactory.createLineBorder(Color.black));
        //this.addMouseListener(this);
        /*this.backBuffer = new BufferedImage(400,400,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) backBuffer.getGraphics();
        g.dispose();
        g = null;
        
        this.buffer = new BufferedImage(400,400,BufferedImage.TYPE_INT_ARGB);
        this.bufferGraphic = (Graphics2D) this.buffer.getGraphics();*/
        
        //this.setOpaque(false);
        try
        {
           img = ImageIO.read(new File(str));  
           //this.setBounds(0,0, 400, 400);
           
        }
        catch(IOException e)
        {
            
        }
    }
    
    public theSpritePieces(String str, point p, colorOfPiexes side)
    {
        //this.view = view;
        this.side = side;
        this.inPlace = new point(p.getX(),p.getY());
        this.desPlace = new point();
        this.wayToGo = new line(this.inPlace,this.desPlace);
        
        /*this.backBuffer = new BufferedImage(400,400,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) backBuffer.getGraphics();
        g.dispose();
        g = null;
        
        this.buffer = new BufferedImage(400,400,BufferedImage.TYPE_INT_ARGB);
        this.bufferGraphic = (Graphics2D) this.buffer.getGraphics();*/
        
        //this.setOpaque(false);
        try
        {
           img = ImageIO.read(new File(str));  
           //this.setBounds(0,0, 400, 400);
           
        }
        catch(IOException e)
        {
            
        }
    }
    
    public boolean isMoveLegal(int xi, int yi, int xj, int yj)
    {
        return true;
    }
    
    public point movePattern(int xi, int yi, int xj, int yj)
    {
        point nextPos = new point(xj,yj);
        
        return nextPos;
    }
    
    
    public kindOfPieces getkind()
    {
        return kindOfPieces.nothingHere;
    }
    
    
    
    public void setDestination(float x, float y)
    {
        this.desPlace.setX(x);
        this.desPlace.setY(y);
        this.wayToGo = new line(this.inPlace,this.desPlace);
    }
    
    public boolean move()
    {
        
        boolean result = this.wayToGo.next();
        this.inPlace = new point(this.wayToGo.getA());
        System.out.printf("\n your place x: "+ this.inPlace.getX()+" y: " + this.inPlace.getY());
        return result;
    }
    
    public void mouseClick(MouseEvent e)
    {
        this.view.propertyChange(new PropertyChangeEvent(this,"Your ganmes", this.inPlace,new Integer(1)));
    }
    
    @Override
    public void run() 
    {
        
    }
    
    
    /*@Override
    public void paint(Graphics g)
    {
        super.paint(g);
    }*/
    
    public void draws(Graphics2D grap)
    {
            AffineTransform t = new AffineTransform();
            t.translate(this.inPlace.getX(), this.inPlace.getY());
            t.scale(1, 1);
            grap.drawImage(this.img,t, view);
    }
    
    /*@Override
    public void mouseClicked(MouseEvent e) {
            System.out.printf("\nmouseClicked:");
        this.view.propertyChange(new PropertyChangeEvent(this,"Your ganmes", this.inPlace,new Integer(1)));
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseEntered(MouseEvent e) {
                    System.out.printf("\nmouseClicked:" + count);
        this.view.propertyChange(new PropertyChangeEvent(this,"Your ganmes", this.inPlace,new Integer(1)));
        count ++;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.printf("\nmouseClicked:" + count);
        this.view.propertyChange(new PropertyChangeEvent(this,"Your ganmes", this.inPlace,new Integer(1)));
        count ++;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
        /**
     * @return the inPlace
     */
    public point getInPlace() {
        return inPlace;
    }

    /**
     * @param inPlace the inPlace to set
     */
    public void setInPlace(point inPlace) {
        this.inPlace = inPlace;
    }

        /**
     * @return the side
     */
    public colorOfPiexes getSide() {
        return side;
    }
}
